import type { ListPageModel, PageModel, UserModel } from '@/api/model/userModel';
import { request } from '@/utils/request';

const Api = {
  CreateUser: '/user/create',
  GetUser: '/user/getList',
  DeleteUser: '/user/delete',
  GetUserInfos: '/user/userInfos',
  UpdateUser: '/user/update',
};

/** 创建用户 */
export const createUser = (data: Partial<UserModel>) => {
  return request.post<boolean>({
    url: Api.CreateUser,
    data,
  });
};

interface IGetUserParamsSchema extends PageModel {
  userName?: string;
  email?: string;
}

/** 获取用户列表 */
export const getUserList = (params: IGetUserParamsSchema) => {
  return request.get<ListPageModel<UserModel>>({
    url: Api.GetUser,
    params,
    withCredentials: true,
  });
};

/** 删除用户 */
export const deleteUser = (userId: string) => {
  return request.delete<ListPageModel<UserModel>>({
    url: Api.DeleteUser,
    params: { userId },
  });
};

/** 根据userId获取用户信息 */
export const getUserInfosById = (userId: string) => {
  return request.get<UserModel>({
    url: Api.GetUserInfos,
    params: { userId },
  });
};

/** 更新用户 */
export const updateUser = (user: Partial<UserModel>) => {
  return request.put<UserModel>({
    url: Api.UpdateUser,
    params: { ...user },
  });
};
